/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package simplemetrosystem.view.canvas.MouseHandlers;

import java.awt.Cursor;
import java.awt.Point;
import java.awt.event.MouseEvent;
import simplemetrosystem.view.MetroFrame;
import simplemetrosystem.view.canvas.CanvasModel;
import simplemetrosystem.view.canvas.MetroCanvas;

/**
 *
 * @author Eric Qian
 */
public class SelectingMouseHandler extends BasicMouseHandler {

    public SelectingMouseHandler(MetroFrame frame) {
        super(frame);
        this.frame = frame;

    }

    @Override
    public void mouseClicked(MouseEvent e) {
        super.mouseClicked(e);

        MetroCanvas canvas = frame.getMetroTabbedPane().getAllLineCanvas();
        CanvasModel model = CanvasModel.getInstance();

        if (!model.isSelectedEmpty()) {
            canvas.setSelectedMouseHandler();
        }
    }

    @Override
    public void mouseEntered(MouseEvent e) {
        super.mouseEntered(e);
        frame.setCursor(crossCursor);
    }

    @Override
    public void mouseExited(MouseEvent e) {
        super.mouseExited(e);
        frame.setCursor(defaultCursor);
    }

    @Override
    public void mousePressed(MouseEvent e) {
        x1 = e.getX();
        y1 = e.getY();
    }

    @Override
    public void mouseReleased(MouseEvent e) {
        CanvasModel model = CanvasModel.getInstance();
        MetroCanvas canvas = frame.getMetroTabbedPane().getAllLineCanvas();

        model.setSelect();
        canvas.setSelected(false);
        canvas.repaint();
        if (!model.isSelectedEmpty()) {
            frame.setCursor(defaultCursor);
            canvas.setSelectedMouseHandler();
        }
    }

    @Override
    public void mouseDragged(MouseEvent e) {
        MetroCanvas canvas = frame.getMetroTabbedPane().getAllLineCanvas();
        int x2 = e.getX();
        int y2 = e.getY();
        canvas.setSelected(true);
        canvas.setSelectRec(Math.min(x1, x2),
                Math.min(y1, y2), Math.abs(x1 - x2), Math.abs(y2 - y1));
        int tx1 = MouseTrans.X(x1);
        int ty1 = MouseTrans.Y(y1);
        x2 = MouseTrans.X(x2);
        y2 = MouseTrans.Y(y2);
        CanvasModel.getInstance().setHighLight(Math.min(tx1, x2),
                Math.min(ty1, y2), Math.abs(tx1 - x2), Math.abs(y2 - ty1));

    }

    @Override
    public void mouseMoved(MouseEvent e) {
        super.mouseMoved(e);
        CanvasModel model = CanvasModel.getInstance();

        int x = MouseTrans.X(e.getX());
        int y = MouseTrans.Y(e.getY());
        if (!model.isEmpty(x, y)) {
            frame.setCursor(handCursor);
        } else {
            frame.setCursor(crossCursor);
        }
    }
    private MetroFrame frame;
    private int x1;
    private int y1;
    private static Cursor crossCursor = new Cursor(Cursor.CROSSHAIR_CURSOR);
}
